package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.exception.ExceptionCast;
import com.atguigu.common.exception.ExceptionCodeEnum;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getCategoryBrandRelationByBrandId(Map<String, Object> params) {

        Long brandId = Long.parseLong((String) params.get("brandId"));

        if (brandId == null) {
            ExceptionCast.exception(ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getMsg(), ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getCode());
        }

        //不要分页
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>();

        wrapper.eq("brand_id", brandId);

        return baseMapper.selectList(wrapper);

    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {

        if (categoryBrandRelation.getBrandId() == null || categoryBrandRelation.getCatelogId() == null) {
            ExceptionCast.exception(ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getMsg(), ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getCode());
        }

        CategoryEntity categoryEntity = categoryService.findOneById(categoryBrandRelation.getCatelogId());
        BrandEntity brandEntity = brandService.findOneById(categoryBrandRelation.getBrandId());
        if (categoryEntity == null || brandEntity == null) {
            ExceptionCast.exception(ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getMsg(), ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getCode());
        }

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        baseMapper.insert(categoryBrandRelation);
    }

    @Override
    public void updateDetailsById(CategoryBrandRelationEntity categoryBrandRelation) {
        if (categoryBrandRelation.getBrandId() == null || categoryBrandRelation.getCatelogId() == null) {
            ExceptionCast.exception(ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getMsg(), ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getCode());
        }

        CategoryEntity categoryEntity = categoryService.findOneById(categoryBrandRelation.getCatelogId());
        BrandEntity brandEntity = brandService.findOneById(categoryBrandRelation.getBrandId());
        if (categoryEntity == null || brandEntity == null) {
            ExceptionCast.exception(ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getMsg(), ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getCode());
        }

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        baseMapper.updateById(categoryBrandRelation);
    }

    @Override
    public CategoryBrandRelationEntity findOneById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void updateBrandNameByBrandId(Long brandId, String brandName) {

        if (StringUtils.isEmpty(brandName) || brandId == null) {
            ExceptionCast.exception(ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getMsg(), ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getCode());
        }

        BrandEntity brandEntity = brandService.findOneById(brandId);

        if (brandEntity == null) {
            ExceptionCast.exception(ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getMsg(), ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getCode());
        }

        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<CategoryBrandRelationEntity>();
        wrapper.eq("brand_id", brandId);

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(brandName);
        baseMapper.update(categoryBrandRelationEntity, wrapper);
    }

    @Override
    public void updateCateNameByCatId(Long catId, String CateName) {

        if (StringUtils.isEmpty(CateName) || catId == null) {
            ExceptionCast.exception(ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getMsg(), ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getCode());
        }

        CategoryEntity categoryEntity = categoryService.findOneById(catId);

        if (categoryEntity == null) {
            ExceptionCast.exception(ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getMsg(), ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getCode());
        }

        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<CategoryBrandRelationEntity>();
        wrapper.eq("catelog_id", catId);

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogName(CateName);
        baseMapper.update(categoryBrandRelationEntity, wrapper);

    }

    @Override
    public void deleteByBrandId(Long brandId) {

        if (brandId == null) {
            ExceptionCast.exception(ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getMsg(), ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getCode());
        }

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>();
        wrapper.eq("brand_id", brandId);
        baseMapper.delete(wrapper);
    }

    @Override
    public void deleteByCatId(Long catId) {
        if (catId == null) {
            ExceptionCast.exception(ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getMsg(), ExceptionCodeEnum.ILLEGAL_ARGS_EXCEPTION.getCode());
        }

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>();
        wrapper.eq("catelog_id", catId);
        baseMapper.delete(wrapper);
    }


}